#ifndef __INCSessionManagerh
#define __INCSessionManagerh
#include <objbase.h>
#include <Session.h>
#include <log.h>

class SessionManager :public objbase
{
private:
    DISABLE_COPY_CLASS(SessionManager);
    SocketHandle m_listenSocket;
    std::vector<std::shared_ptr<Session>> m_sessions;
    FUNCPTR m_readCallback;
    FUNCPTR m_closeCallback;
    std::atomic<bool> m_isRunning;

    static const int MAX_FDS = 8;

public:
    DECLARE_CTOR_DTOR(SessionManager);
    OBJBASE_EXTERN_MEMBER_IMPL();
    STATUS init(int port);
    STATUS runEventLoop();
    STATUS setReadCallback(FUNCPTR callback);
    STATUS setCloseCallback(FUNCPTR callback);
    STATUS reportEvent(const std::string & contents);

private:
    PROCESS_DESC(sysShutdown);

private:
    STATUS createListenSocket(int port);
    void handleConnectEvents(fd_set& readSet);
    void handleReadEvents(fd_set& readSet);
    void handleWriteEvents(fd_set& writeSet);
    void handleTimeOutEvents();
    void handleClosedEvents();
    void clearSessions();
    void loginAuth(SessionPtr session);
    void sendAuthPrompt(SessionPtr session);
};

#endif // __INCSessionManagerh